//
//  GuideViewController.m
//  CarRental
//
//  Created by wmwMAC on 17/6/7.
//  Copyright © 2017年 Jack. All rights reserved.
//

#import "GuideViewController.h"

#import "LogInViewController.h"

#import "SetViewController.h"

@interface GuideViewController ()<UIScrollViewDelegate>
{
    // 创建页码控制器
    UIPageControl *pageControl;
    // 判断是否是第一次进入应用
    BOOL flag;
}
@property (nonatomic, strong) NSMutableArray *picArr;
@property (nonatomic, strong) UIScrollView *myScrollView;
@property (nonatomic, strong) UIButton *button;
@end

@implementation GuideViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.picArr = [NSMutableArray array];
    
    [self getYindaoye];
    
    [self addContentUI];
    
    
    
  //http://anju.m0571.com/Public/uploads/1498644461.png
    
}
-(void)addContentUI{
   self.myScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kUIScreenWidth, kUIScreenHeight)];
    for (int i=0; i<3; i++) {
        
        
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(kUIScreenWidth * i, 0, kUIScreenWidth, kUIScreenHeight)];
        // 在最后一页创建按钮
        if (i == 2) {
            // 必须设置用户交互 否则按键无法操作
            imageView.userInteractionEnabled = YES;
             _button= [UIButton buttonWithType:UIButtonTypeSystem];
            _button.frame = CGRectMake(kUIScreenWidth / 3, kUIScreenHeight * 7.47 / 9, kUIScreenWidth / 3, kUIScreenHeight / 16);
            [_button setTitle:@"" forState:UIControlStateNormal];
            [_button addTarget:self action:@selector(go:) forControlEvents:UIControlEventTouchUpInside];
            
            _button.backgroundColor = [UIColor clearColor];
            [imageView addSubview:_button];
//            imageView.backgroundColor = [UIColor redColor];
        }
        if (_picArr.count) {
            NSString *imgStr = [NSString stringWithFormat:@"http://%@",_picArr[i]];
            [imageView sd_setImageWithURL:[NSURL URLWithString:imgStr] placeholderImage:[UIImage imageNamed:@"break"]];
        }else{
            imageView.image = [UIImage imageNamed:@"break"];
            [_button setTitle:@"立即体验" forState:UIControlStateNormal];
            
        }
        

        [_myScrollView addSubview:imageView];
    }
    _myScrollView.bounces = NO;
    _myScrollView.pagingEnabled = YES;
    _myScrollView.showsHorizontalScrollIndicator = NO;
    _myScrollView.contentSize = CGSizeMake(kUIScreenWidth * 3, kUIScreenHeight);
    _myScrollView.delegate = self;
    [self.view addSubview:_myScrollView];
    
    pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(kUIScreenWidth / 3, kUIScreenHeight * 15 / 16, kUIScreenWidth / 3, kUIScreenHeight / 16)];
    // 设置页数
    pageControl.numberOfPages = 0;
    // 设置页码的点的颜色
    pageControl.pageIndicatorTintColor = [UIColor clearColor];
    // 设置当前页码的点颜色
    pageControl.currentPageIndicatorTintColor = [UIColor clearColor];
    
    [self.view addSubview:pageControl];
}


#pragma mark - UIScrollViewDelegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    // 计算当前在第几页
    pageControl.currentPage = (NSInteger)(scrollView.contentOffset.x / [UIScreen mainScreen].bounds.size.width);
}

// 点击按钮保存数据并切换根视图控制器
- (void) go:(UIButton *)sender{
    flag = YES;
    NSUserDefaults *useDef = [NSUserDefaults standardUserDefaults];
    
    
    // 使用 NSUserDefaults 读取用户数据
    if (![useDef boolForKey:@"notFirst"]) {
        // 保存用户数据
        [useDef setBool:flag forKey:@"notFirst"];
        [useDef synchronize];
        
        
        LogInViewController *rootVC = [[LogInViewController alloc] init];
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:rootVC];
        self.view.window.rootViewController = nav;
        
    }else{
        

        [self.navigationController popViewControllerAnimated:YES];
    }
    
    
    
    // 切换根视图控制器
    
    
    
    
}



-(void)getYindaoye{
    
    NSString *url = [NSString stringWithFormat:YINDAOYE,RealmName];
    [HttpRequest postWithURLString:url parameters:nil success:^(id responseObject) {
        
        NSLog(@"%@",responseObject);
        _picArr = [responseObject objectForKey:@"pic"];
        
        [self.myScrollView removeFromSuperview];
        [self addContentUI];
    } failure:^(NSError *error) {
        
    }];
    
}




-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = NO;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
